package com.course.business.controller.web.discusspost;


import com.course.business.controller.web.discusspost.event.EventProducer;
import com.course.server.domain.discusspost.Comment;
import com.course.server.domain.discusspost.DiscussPost;
import com.course.server.domain.discusspost.Event;
import com.course.server.dto.ResponseDto;
import com.course.server.mapper.my.MyDiscussPostMapper;
import com.course.server.service.discusspost.CommentService;
import com.course.server.service.discusspost.DiscussPostService;
import com.course.server.util.CommunityConstant;
import com.course.server.util.HostHolder;
import com.course.server.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/web/comment")
public class CommentController implements CommunityConstant {

    @Autowired
    private CommentService commentService;

    @Autowired
    private HostHolder hostHolder;

    @Autowired
    private EventProducer eventProducer;

    @Autowired
    private DiscussPostService discussPostService;

    @Resource(name="myRedisTemplate")
    private RedisTemplate redisTemplate;

    @Autowired
    private MyDiscussPostMapper myDiscussPostMapper;

    @RequestMapping(path = "/add/{discussPostId}", method = RequestMethod.POST)
    public ResponseDto addComment(@PathVariable("discussPostId") String discussPostId,@RequestBody Comment comment) {
        comment.setUserId(hostHolder.getMember().getId());
        comment.setStatus(0);
        comment.setCreateTime(new Date());
        commentService.addComment(comment);

        //系统发个通知给 发帖人/写回帖的人，“用户 aaa 评论了你的帖子/回复, 点击查看 !”。实际上是往message表里插入一条数据。
        // 触发评论事件
        Event event = new Event()
                .setTopic(TOPIC_COMMENT)
                .setUserId(hostHolder.getMember().getId())
                .setEntityType(comment.getEntityType())
                .setEntityId(comment.getEntityId())
                .setData("postId", discussPostId);
        if (comment.getEntityType().equals(ENTITY_TYPE_POST)) {
            DiscussPost target = myDiscussPostMapper.selectDiscussPostById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());//给发帖人
        } else if (comment.getEntityType().equals(ENTITY_TYPE_COMMENT)) {
            Comment target = commentService.findCommentById(comment.getEntityId());
            event.setEntityUserId(target.getUserId());//给写回帖的人
        }
        eventProducer.fireEvent(event);

        if (comment.getEntityType().equals(ENTITY_TYPE_POST)) {
            // 触发发帖事件
            //更新elasticsearch中帖子的数据
            event = new Event()
                    .setTopic(TOPIC_PUBLISH)
                    .setUserId(comment.getUserId())
                    .setEntityType(ENTITY_TYPE_POST)
                    .setEntityId(discussPostId);
            eventProducer.fireEvent(event);
            // 计算帖子分数
            String redisKey = RedisKeyUtil.getPostScoreKey();
            redisTemplate.opsForSet().add(redisKey, discussPostId);
        }

        return new ResponseDto();
    }

}
